﻿//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Workflow
//  File name:	SPAssignmentActivity.cs
//  Developer:	Solomatov Igor
//  Created:	03.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint.Workflow;
using Smart.Common;

namespace Smart.Workflow.WorkflowActions
{
	public partial class SPAssignmentActivity : SPSequenceActivity
	{
		#region Properties

		public static DependencyProperty InstanceProperty =
			DependencyProperty.Register("Instance", typeof(AssignmentInstance), typeof(SPAssignmentActivity));

		[Category("Required properties")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[ValidationOption(ValidationOption.Required)]
		public AssignmentInstance Instance
		{
			get { return ((AssignmentInstance)(GetValue(InstanceProperty))); }
			set { SetValue(InstanceProperty, value); }
		}

		public static DependencyProperty TaskIdProperty =
			DependencyProperty.Register("TaskId", typeof(Guid), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public Guid TaskId
		{
			get { return ((Guid)(GetValue(TaskIdProperty))); }
			set { SetValue(TaskIdProperty, value); }
		}

		public static DependencyProperty TaskPropertiesProperty =
			DependencyProperty.Register("TaskProperties", typeof(SPWorkflowTaskProperties), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public SPWorkflowTaskProperties TaskProperties
		{
			get { return ((SPWorkflowTaskProperties)(GetValue(TaskPropertiesProperty))); }
			set { SetValue(TaskPropertiesProperty, value); }
		}

		public static DependencyProperty ContentTypeIdProperty =
			DependencyProperty.Register("ContentTypeId", typeof(String), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public String ContentTypeId
		{
			get { return ((string)(GetValue(ContentTypeIdProperty))); }
			set { SetValue(ContentTypeIdProperty, value); }
		}

		public static DependencyProperty SpecialPermissionsProperty =
			DependencyProperty.Register("SpecialPermissions", typeof(HybridDictionary), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public HybridDictionary SpecialPermissions
		{
			get { return ((HybridDictionary)(GetValue(SpecialPermissionsProperty))); }
			set { SetValue(SpecialPermissionsProperty, value); }
		}

		public static DependencyProperty ListItemIdProperty =
			DependencyProperty.Register("ListItemId", typeof(int), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public int ListItemId
		{
			get { return ((int)(GetValue(ListItemIdProperty))); }
			set { SetValue(ListItemIdProperty, value); }
		}

		public static DependencyProperty ExecutorProperty =
			DependencyProperty.Register("Executor", typeof(string), typeof(SPAssignmentActivity));

		[Category("Properties")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public string Executor
		{
			get { return ((string)(GetValue(ExecutorProperty))); }
			set { SetValue(ExecutorProperty, value); }
		}

		public static DependencyProperty ExecutorIdProperty =
			DependencyProperty.Register("ExecutorId", typeof(int), typeof(SPAssignmentActivity));

		[Category("Properties")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public int ExecutorId
		{
			get { return ((int)(GetValue(ExecutorIdProperty))); }
			set { SetValue(ExecutorIdProperty, value); }
		}

		public static DependencyProperty AfterPropertiesProperty =
			DependencyProperty.Register("AfterProperties", typeof(SPWorkflowTaskProperties), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public SPWorkflowTaskProperties AfterProperties
		{
			get { return ((SPWorkflowTaskProperties)(GetValue(AfterPropertiesProperty))); }
			set { SetValue(AfterPropertiesProperty, value); }
		}

		public static DependencyProperty BeforePropertiesProperty =
			DependencyProperty.Register("BeforeProperties", typeof(SPWorkflowTaskProperties), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public SPWorkflowTaskProperties BeforeProperties
		{
			get { return ((SPWorkflowTaskProperties)(GetValue(BeforePropertiesProperty))); }
			set { SetValue(BeforePropertiesProperty, value); }
		}

		public static DependencyProperty TaskOutcomeProperty =
			DependencyProperty.Register("TaskOutcome", typeof(String), typeof(SPAssignmentActivity));

		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		[Browsable(true)]
		[Category("Properties")]
		public String TaskOutcome
		{
			get { return ((string)(GetValue(TaskOutcomeProperty))); }
			set { SetValue(TaskOutcomeProperty, value); }
		}

		[Browsable(false)]
		public bool IsExpirationEnabled { get; set; }

		[Browsable(false)]
		public bool IsCompleted { get; set; }

		[Browsable(false)]
		public bool IsDeleted { get; set; }

		[Browsable(false)]
		public bool IsExpired { get; set; }

		#endregion

		#region Events

		public static DependencyProperty AssignmentInitializingEvent =
			DependencyProperty.Register("AssignmentInitializing", typeof(EventHandler<AssignmentActivityEventArgs>), typeof(SPAssignmentActivity));

		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentActivityEventArgs> AssignmentInitializing
		{
			add { AddHandler(AssignmentInitializingEvent, value); }
			remove { RemoveHandler(AssignmentInitializingEvent, value); }
		}

		public void RaiseAssignmentInitializingEvent(AssignmentActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentInitializingEvent, this, e);
		}

		public static DependencyProperty AssignmentTaskCreatingEvent =
			DependencyProperty.Register("AssignmentTaskCreating", typeof(EventHandler<AssignmentActivityEventArgs>), typeof(SPAssignmentActivity));

		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentActivityEventArgs> AssignmentTaskCreating
		{
			add { AddHandler(AssignmentTaskCreatingEvent, value); }
			remove { RemoveHandler(AssignmentTaskCreatingEvent, value); }
		}

		public void RaiseAssignmentTaskCreatingEvent(AssignmentActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentTaskCreatingEvent, this, e);
		}

		public static DependencyProperty AssignmentTaskChangedEvent =
			DependencyProperty.Register("AssignmentTaskChanged", typeof(EventHandler<AssignmentTaskActivityEventArgs>), typeof(SPAssignmentActivity));

		[Description("AssignmentTaskChanged event")]
		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentTaskActivityEventArgs> AssignmentTaskChanged
		{
			add { AddHandler(AssignmentTaskChangedEvent, value); }
			remove { RemoveHandler(AssignmentTaskChangedEvent, value); }
		}

		public void RaiseAssignmentTaskChangedEvent(AssignmentTaskActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentTaskChangedEvent, this, e);
		}

		public static DependencyProperty AssignmentTaskCompletingEvent =
			DependencyProperty.Register("AssignmentTaskCompleting", typeof(EventHandler<AssignmentTaskActivityEventArgs>), typeof(SPAssignmentActivity));

		[Description("AssignmentTaskCompleting event")]
		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentTaskActivityEventArgs> AssignmentTaskCompleting
		{
			add { AddHandler(AssignmentTaskCompletingEvent, value); }
			remove { RemoveHandler(AssignmentTaskCompletingEvent, value); }
		}

		public void RaiseAssignmentTaskCompletingEvent(AssignmentTaskActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentTaskCompletingEvent, this, e);
		}

		public static DependencyProperty AssignmentTaskDeletedEvent =
			DependencyProperty.Register("AssignmentTaskDeleted", typeof(EventHandler<AssignmentTaskActivityEventArgs>), typeof(SPAssignmentActivity));

		[Description("AssignmentTaskDeleted event")]
		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentTaskActivityEventArgs> AssignmentTaskDeleted
		{
			add { AddHandler(AssignmentTaskDeletedEvent, value); }
			remove { RemoveHandler(AssignmentTaskDeletedEvent, value); }
		}

		public void RaiseAssignmentTaskDeletedEvent(AssignmentTaskActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentTaskDeletedEvent, this, e);
		}

		public static DependencyProperty AssignmentTaskExpiredEvent =
			DependencyProperty.Register("AssignmentTaskExpired", typeof(EventHandler<AssignmentTaskActivityEventArgs>), typeof(SPAssignmentActivity));

		[Description("AssignmentTaskExpired event")]
		[Category("Events")]
		[Browsable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
		public event EventHandler<AssignmentTaskActivityEventArgs> AssignmentTaskExpired
		{
			add { AddHandler(AssignmentTaskExpiredEvent, value); }
			remove { RemoveHandler(AssignmentTaskExpiredEvent, value); }
		}

		public void RaiseAssignmentTaskExpiredEvent(AssignmentTaskActivityEventArgs e)
		{
			RaiseGenericEvent(AssignmentTaskExpiredEvent, this, e);
		}

		#endregion

		public SPAssignmentActivity()
		{
			InitializeComponent();
		}

		protected virtual void Initialize(object sender, EventArgs e)
		{
			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
				Instance.Setup(GetService());

				RaiseAssignmentInitializingEvent(new AssignmentActivityEventArgs(this, Instance, this));
			}
		}

		protected virtual void TaskCreating(object sender, EventArgs e)
		{
			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
				TaskId = Instance.TaskId;
				TaskProperties = Instance.GetTaskProperties();
				SpecialPermissions = Instance.GetTaskSpecialPermissions();
				ContentTypeId = Instance.GetTaskContentTypeId(WorkflowProperties.TaskList).ToString();

				IsExpirationEnabled = Instance.DueDate.IsNormal();

				RaiseAssignmentTaskCreatingEvent(
					new AssignmentTaskActivityEventArgs((Activity)sender, Instance, this, TaskId, Executor, null, null, TaskProperties));
			}
		}

		protected virtual void TaskChanged(object sender, ExternalDataEventArgs e)
		{
			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
				var taskItem = WorkflowProperties.TaskList.GetItem(ListItemId);

#warning NOT IMPLEMENTED!

				var ae =
					new AssignmentTaskActivityEventArgs((Activity)sender, Instance, this, TaskId, Executor, taskItem, BeforeProperties, AfterProperties)
						{
							CompleteTask = WH.IsTaskCompleted(AfterProperties)
						};

				RaiseAssignmentTaskChangedEvent(ae);

				if (ae.CompleteTask)
				{
					IsCompleted = true;
					IsExpirationEnabled = false;
				}
			}
		}

		protected virtual void TaskCompleting(object sender, EventArgs e)
		{
			IsCompleted = true;
			IsExpirationEnabled = false;

			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
				var taskItem = WorkflowProperties.TaskList.GetItem(ListItemId);

#warning NOT IMPLEMENTED!

				RaiseAssignmentTaskCompletingEvent(
					new AssignmentTaskActivityEventArgs((Activity)sender, Instance, this, TaskId, Executor, taskItem, null, null));
			}
		}

		protected virtual void TaskDeleted(object sender, ExternalDataEventArgs e)
		{
			IsCompleted = true;
			IsDeleted = true;
			IsExpirationEnabled = false;

			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
#warning NOT IMPLEMENTED!

				RaiseAssignmentTaskDeletedEvent(
					new AssignmentTaskActivityEventArgs((Activity)sender, Instance, this, TaskId, Executor, null, null, AfterProperties));
			}
		}

		protected virtual void TaskExpired(object sender, EventArgs e)
		{
			IsExpired = true;
			IsExpirationEnabled = false;

			using (SPH.SetContext(WorkflowProperties.Site, WorkflowProperties.Web))
			{
				var taskItem = WorkflowProperties.TaskList.GetItem(ListItemId);

#warning NOT IMPLEMENTED!

				RaiseAssignmentTaskExpiredEvent(
					new AssignmentTaskActivityEventArgs((Activity)sender, Instance, this, TaskId, Executor, taskItem, null, null));
			}
		}
	}
}